﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntGuayasSLA.BL;
using Ext.Net;
using System.Globalization;
using EntGuayasSLA.BL.Listas;

namespace EGuayas.Web.Expedientes
{

    /// <summary>
    /// ~/EGlobal/pgGeoreferencia.aspx
    /// </summary>
    public partial class pgGeoreferencia : pgBase
    {
        protected string ExpedienteIdTmp
        {
            get
            {
                if (Session[Request["pgName"] + "_ExpedienteIdTmp"] == null)
                    return "";
                else
                    return (Session[Request["pgName"] + "_ExpedienteIdTmp"].ToString());
            }
            set
            {
                Session[Request["pgName"] + "_ExpedienteIdTmp"] = value;
            }
        }
        protected string EventoId
        {
            set
            {
                Session[Request["pgName"] + "_pgGeoreferencia"] = value;
            }
            get
            {
                return (Session[Request["pgName"] + "_pgGeoreferencia"].ToString());               
            }
        }
        protected Boolean IfInsert
        {
           
            get
            {
                var val = (EventoId == "S");
                return (val);
            }
        }
       
        protected void Page_Load(object sender, EventArgs e)
        {
          try
          { 
            if (!X.IsAjaxRequest)
            {
                //Setear variables de request
                if (Request["hdIfCreateExp"] != null)
                {
                    this.hdIfCreateExp.SetValue(Request["hdIfCreateExp"]);                
                }
                //
                if (Request["pgIcon"] != null && Request["pgIcon"] == "A")
                {
                    this.btnSave.Icon = Icon.Accept;
                    this.btnSave.Text = "Aceptar";
                
                }
                this.hdIdPage.SetValue(Request["pgName"]);
                //
                this.hdExpGeo.SetValue(ExpedienteIdTmp);
                UbicacionLista data = UbicacionLista.GetUbicacionLista(ExpedienteIdTmp);
                this.stoRef.DataSource = data;
                this.stoRef.DataBind();

                if (ExpedienteIdTmp == "")
                {                 
                    this.btnAddUpdate.Hide();
                    this.btnAdd.Show();
                }
                else
                {
                    this.btnAddUpdate.Show();
                    this.btnAdd.Hide();                                      
                }
                EventoId =(data.Count==0)? "N":"S";
                X.Js.Call("pgFn.setPointPaint", data);

                if (IfInsert)
                {
                    btnFlag.Enable();
                    btnSave.Enable();
                    btnRemove.Enable();
                   
                }
                else
                {
                    btnFlag.Disable();                    
                    btnSave.Disable();
                    btnRemove.Disable();
                }
               
             
            }
          }
          catch (Exception ex)
          {
              RegistrarLog(ex);
          }
        }
       
        protected void btnGuardar_Click(object sender, DirectEventArgs e)
        {
            try
            {
                string txtMsg = "Almacenados correctamente";
                string gridJson = e.ExtraParams["GridData"];
                Dictionary<string, string>[] gridData = JSON.Deserialize<Dictionary<string, string>[]>(gridJson);

                string sepDec = System.Globalization.NumberFormatInfo.InvariantInfo.CurrencyDecimalSeparator;
                if (IfInsert)
                {
                    Ubicacion objUbica = Ubicacion.GetEditableRoot(Guid.Parse(ExpedienteIdTmp));
                    objUbica.Delete();
                    objUbica.Save();
                    txtMsg = "Modificados correctamente";
                }
                int i = 0;
                while (i < gridData.Length)
                {
                    Ubicacion objUbica = Ubicacion.NewEditableRoot();
                    objUbica.Orden = i + 1;
                    objUbica.Id = Guid.NewGuid();
                    var val1 = gridData[i];
                    objUbica.Latitud = val1["Latitud"].Trim();
                    objUbica.Longitud = val1["Longitud"].Trim();
                    objUbica.ExpedienteId = Guid.Parse(ExpedienteIdTmp);

                    objUbica.Tipo = "Geo";
                    objUbica.Save();
                    i += 1;

                }
                btnSave.Disable();
                btnRemove.Disable();
                btnFlag.Enable();
             
                Boolean bAddEndExp = Convert.ToBoolean(e.ExtraParams["IfAddExp"]);
                if (bAddEndExp == true)
                {
                    X.Js.Call("pgFn.theEndExpediente");
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "Información",
                        Message = string.Format("{0} <br/><b>{1}</b>", "Puntos de refencia agregados", txtMsg)
                    });
                
                }
            }
            catch (Exception ex)
            {
                RegistrarLog(ex);
            }
        }
           
    }
}